<template>
	<a-modal title="产品列表" :width="1300" :visible="visible" :confirmLoading="confirmLoading" :footer="null"
		@ok="handleSubmit" @cancel="handleCancel">
		<a-card :bordered="false">

			<div class="table-operator">
				<a-button type="primary" @click="handleSubmit">确认入库</a-button>
			</div>

			<s-table ref="table" size="default" :columns="columns" :data="loadData" :alert="true"
				:rowKey="(record) => record.code" :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
				:showPagination="false">
				<span v-if="record.executeStatus == $businessEnum.EXECUTE_STATUS_ENUM.IN_PROGRESS.id" slot="remark"
					slot-scope="text, record">
					<a-tooltip placement="topLeft" :title="record.material.remark">
						{{ record.material.remark }}
					</a-tooltip>
				</span>

				<template slot="storehousePositionRemark" slot-scope="text, record">
					<a-input v-if="record.executeStatus == $businessEnum.EXECUTE_STATUS_ENUM.IN_PROGRESS.id"
						v-model:value="record.storehousePositionRemark" />
				</template>
				<!-- 箱数 -->
				<!-- <template slot="boxNumber" slot-scope="text, record">
					<a-input-number :min="1" v-model:value="record.boxNumber" />					
				</template> -->

				<span slot="action" slot-scope="text, record">
					<a v-if="record.executeStatus == $businessEnum.EXECUTE_STATUS_ENUM.IN_PROGRESS.id"
						@click="handleRowSubmit(record)">确认入库</a>
					<span v-else>已入库</span>
				</span>
			</s-table>

		</a-card>
	</a-modal>
</template>

<script>
import { STable } from '@/components'
import { page } from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'
import { handleProductionInStoreOrderProductInStore, handleProductionOrderProductComplete } from '@/api/modular/main/emsorder/emsOrderManage'

export default {
	components: {
		STable,
	},

	data: function () {
		return {
			title: null,
			advanced: false, // 高级搜索 展开/关闭
			queryParam: {},  // 查询参数
			columns: [
				{
					title: '产品编号',
					dataIndex: 'materialCode'
				},
				{
					title: '产品名称',
					dataIndex: 'materialName'
				},
				{
					title: '待入库数量',
					dataIndex: '',
					customRender: (text, record, index) => `${record.number - record.completedNumber}`
				},
				{
					title: '库位',
					dataIndex: 'material.orgIdStorehousePositionName',
				},
				// {
				// 	title: '箱数',
				// 	dataIndex: 'boxNumber',
				// 	scopedSlots: { customRender: 'boxNumber' }
				// },
				{
					title: '备注',
					dataIndex: 'remark',
					ellipsis: true,
					scopedSlots: { customRender: 'remark' }
				},
				{
					title: '库位备注',
					dataIndex: '',
					scopedSlots: { customRender: 'storehousePositionRemark' }
				},
				{
					title: '状态',
					dataIndex: 'executeStatus',
					customRender: (text, record, index) => this.$util.findExecuteStatusById(text)?.name || '未知状态'
				},
				// {
				//   title: '入库人员',
				//   dataIndex: 'material.updateUserName'
				// },
				// {
				//   title: '入库时间',
				//   dataIndex: 'material.updateTime'
				// },
				{
					title: '操作',
					dataIndex: 'action',
					width: '150px',
					scopedSlots: { customRender: 'action' }
				}
			],
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				return page(Object.assign(parameter, this.queryParam)).then((res) => {
					res.data.rows.forEach(record => {
						// record.storehousePositionRemark = record.material.storehousePositionRemark
						// record.remark = record.material.remark
					})
					return res.data
				})
			},
			selectedRowKeys: [],
			selectedRows: [],
			defaultExpandedKeys: [],
			visible: false,
			confirmLoading: false,
			emsOrder: null,
			viewState: '',
			extra: {},
		}
	},
	computed: {},
	methods: {
		show(record) {
			this.visible = true
			this.emsOrder = record
			this.queryParam = {}
			this.queryParam.orderId = record.orderId
			this.queryParam.pageSize = -1

			setTimeout(() => {
				this.handleOk()
				this.$refs.table.clearSelected()
			}, 100)
		},
		toggleAdvanced() {
			this.advanced = !this.advanced
		},
		handleOk() {
			this.$refs.table.refresh()
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},
		handleSubmit() {
			this.handleInStore(this.selectedRows)
		},
		handleCancel() {
			this.visible = false
		},
		handleRowSubmit(record) {
			this.handleInStore([record])
		},
		handleInStore(rows) {
			if (rows.length == 0) {
				this.$message.error('请选择后再操作')
				return
			}
			let param = Object.assign({}, this.emsOrder, { orderMaterials: rows })
			handleProductionInStoreOrderProductInStore(param).then(res => {
				this.visible = false
				this.handleOk()
				this.$emit('ok')
			})
		},
	}
}
</script>

<style lang="less">
.table-operator {
	margin-bottom: 18px;
}

button {
	margin-right: 8px;
}
</style>
